$(function () {
  $('#bedNO').select2();
	  $("#bedNO").val("-1");
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/remind/list',
        datatype: "json",
        colModel: [			
		    { label: '任务ID', name: 'id_', width: 60, key: true,hidden:true },
		/*	{ label: 'bean名称', name: 'beanName', width: 100 },
			{ label: '方法名称', name: 'methodName', width: 100 }, */
        	{ label: '患者床号', name: 'bedNO', width: 100 },
        	{ label: '提醒类型', name: 'remindType', width: 100,formatter:function(cellvalue, options, rowObject){
        		if(rowObject.remindType==0){
        			return  '点餐提醒';
        		}else if(rowObject.remindType==1){
        			
        			return '吃药提醒';
        		}else{
        			return '康复活动提醒';
        			
        		}
        		
        	} },
        	{ label: '消息消息标题', name: 'params', width: 100 },
        	{ label: '消息内容', name: 'content', width: 100 },
			{ label: 'cron时间表达式 ', name: 'cronExpression', width: 100 },
			{ label: '备注 ', name: 'remark', width: 100 },
			{ label: '状态', name: 'status', width: 60, formatter: function(value, options, row){
				return value === 0 ? 
					'<span class="label label-success">正常</span>' : 
					'<span class="label label-danger">暂停</span>';
			}}
        ],
		viewrecords: true,
        
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var setting = {
	    data: {
	        simpleData: {
	            enable: true,
	            idKey: "deptId",
	            pIdKey: "parentId",
	            rootPId: -1
	        },
	        key: {
	            url:"nourl"
	        }
	    }
	};
	var settingSick = {
		    data: {
		        simpleData: {
		            enable: true,
		            idKey: "deptId",
//		            pIdKey: "null",
		            rootPId: -1
		        },
		        key: {
		            url:"nourl"
		        }
		    }
		};
	var ztree;
	var ztreeSick;

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
//			beanName: null,
			bedNO: null
		},
		showList: true,
		title: null,
		schedule: {},
		remindTypeList: [  
	      { text: '点餐提醒', value: 0 },  
	      { text: '吃药提醒', value: 1 },  
	      { text: '康复活动提醒', value: 2 } 
	    ]
		,
	    bedNOList:[
	    	/* { text: 'nurse', value: 'nurse' },  
		      { text: 'BJ03', value: 'BJ03' },  
		      { text: 'nurse', value:'nurse' }, 
		      { text: 'BJ05', value: 'BJ05' },  
		      { text: 'BJ06', value: 'BJ06' },  
		      { text: 'BJ07', value: 'BJ07' } */
	    ],
	  /*  sickAreaNOList:[]*/
	},
	methods: {
		query: function () {
			vm.reload();
		},
		queryBedNOList: function () {//查询床号List
			$.ajax({
				type: "POST",
			    url: baseURL + "sys/remind/queryBedNOList",
                contentType: "application/json",
			    data: JSON.stringify(vm.schedule),
			    success: function(r){
					if(r.code == 0){
						vm.bedNOList=r.bedNOList;
						$("#bedNO").val(r.bedNOList);
					}else{
						alert(r.msg);
					}
				}
			});
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
//			vm.schedule = {};
//			vm.schedule.remindType=0;
		/*	vm.schedule.bedNO=-1;
		   $("#bedNO").val("-1");*/
//			vm.queryBedNOList();
			vm.schedule.cronExpression =$("#cron").val();
			vm.schedule = {deptName:null, deptId:null, sickAreaNO:null,sickAreaName:null,bedNO:-1,remindType:0};
			vm.getDept();
		},
		update: function () {
			var jobId = getSelectedRow();
			if(jobId == null){
				return ;
			}
			vm.queryBedNOList();
			vm.showList = false;
            vm.title = "修改";
			$.get(baseURL + "sys/remind/info/"+jobId, function(r){
				vm.showList = false;
                vm.title = "修改";
				vm.schedule = r.schedule;
				 vm.getDeptUpdate();
	             vm.getDeptSickUpdate();
			});
			
		},
		saveOrUpdate: function (event) {
			var url = vm.schedule.id_ == null ? "sys/remind/save" : "sys/remind/update";
			vm.schedule.cronExpression =$("#cron").val();
			var bedNO=$("#bedNO").val();
			if(bedNO=="-1"){
				confirm('如果不选中的床号，默认您所属根据组织下发提醒信息？', function(){
					$.ajax({
						type: "POST",
					    url: baseURL + url,
		                contentType: "application/json",
					    data: JSON.stringify(vm.schedule),
					    success: function(r){
					    	if(r.code === 0){
								alert('操作成功', function(){
									vm.reload();
								});
							}else{
								alert(r.msg);
							}
						}
					});
				});
				
			}else{
				vm.schedule.bedNO=bedNO;
				$.ajax({
					type: "POST",
					url: baseURL + url,
					contentType: "application/json",
					data: JSON.stringify(vm.schedule),
					success: function(r){
						if(r.code === 0){
							alert('操作成功', function(){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
				
			}
			
		},
		del: function () {
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/remind/delete",
                    contentType: "application/json",
				    data: JSON.stringify(jobIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		pause: function () {
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}
			
			confirm('确定要暂停选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/remind/pause",
                    contentType: "application/json",
				    data: JSON.stringify(jobIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		resume: function () {
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}
			
			confirm('确定要恢复选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/remind/resume",
                    contentType: "application/json",
				    data: JSON.stringify(jobIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		runOnce: function () {
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}
			
			confirm('确定要立即执行选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/remind/run",
                    contentType: "application/json",
				    data: JSON.stringify(jobIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
//                postData:{'beanName': vm.q.beanName},
                postData:{'bedNO': vm.q.bedNO},
                page:page 
            }).trigger("reloadGrid");
		},
		  getDept: function(){
	            //加载组织树,非病区
	            $.get(baseURL + "sys/dept/listNotSick", function(r){
	                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
	                var node = ztree.getNodeByParam("deptId", vm.schedule.deptId);
	                if(node != null){
	                    ztree.selectNode(node);
	                    vm.schedule.deptName = node[0].name;
	                	vm.schedule.deptId=node[0].deptId;
					}
	            })
	        },
	        getDeptUpdate: function(){
	        	//加载组织树,非病区
	        	$.get(baseURL + "sys/dept/listNotSick", function(r){
	        		ztree = $.fn.zTree.init($("#deptTree"), setting, r);
	        		var node = ztree.getNodeByParam("deptId", vm.schedule.deptId);
	        		if(node != null){
	        			ztree.selectNode(node);
	        			vm.schedule.deptName = node.name;
	        			vm.schedule.deptId=node.deptId;
	        			
	        			/*vm.pro.sickAreaNO=node.sickAreaNO;
	        			vm.pro.sickAreaName=node.sickAreaName;*/
	        		}
	        		
	        	})
	        },
	        getDeptSick: function(){
	        	if(vm.schedule.deptId==null){
	        		alert("请先选择组织");
	        		return;
	        	}
	            //加载组织树
	            $.get(baseURL + "sys/dept/listSick/"+vm.schedule.deptId+"", function(r){
	            	ztreeSick = $.fn.zTree.init($("#deptTreeSick"), settingSick, r);
	                var node = ztreeSick.getNodeByParam("deptId", vm.schedule.sickAreaNO);
	                if(node!=null){
	                	ztreeSick.selectNode(node);
	                	vm.schedule.sickAreaNO=node[0].sickAreaNO;
	                	vm.schedule.sickAreaName=node[0].name;
	                   
	                }
	                   
	            })
	        },
	        getDeptSickUpdate: function(){
	        	if(vm.schedule.deptId==null){
	        		alert("请先选择组织");
	        		return;
	        	}
	        	//加载组织树
	        	$.get(baseURL + "sys/dept/listSick/"+vm.schedule.deptId+"", function(r){
	        		ztreeSick = $.fn.zTree.init($("#deptTreeSick"), settingSick, r);
	        		var node = ztreeSick.getNodeByParam("deptId", vm.schedule.sickAreaNO);
	        		if(node!=null){
	        			ztreeSick.selectNode(node);
	        			vm.schedule.sickAreaNO=node.deptId;
	        			vm.schedule.sickAreaName=node.name;
	        			vm.queryBedNOList();
	        		}
	        		
	        	})
	        },
	   	 
	        deptTree: function(){
	            layer.open({
	                type: 1,
	                offset: '50px',
	                skin: 'layui-layer-molv',
	                title: "选择组织",
	                area: ['300px', '450px'],
	                shade: 0,
	                shadeClose: false,
	                content: jQuery("#deptLayer"),
	                btn: ['确定', '取消'],
	                btn1: function (index) {
	                    var node = ztree.getSelectedNodes();
	                    //选择上级组织
	                        //选择上级组织
		                    if(node.length>0){
		                    	vm.schedule.deptId = node[0].deptId;
		                    	vm.schedule.deptName = node[0].name;
		                    }
	                    /*    if(vm.schedule.sickAreaNO!=null){
	                        	vm.schedule.sickAreaNO=null;
	                        	  vm.schedule.sickAreaName=null;
	                        }*/

	                    layer.close(index);
	                   
	                }
	            });
	        },
	        deptTreeSick: function(){
	        	if(vm.schedule.deptId==null){
	        		alert("请先选择组织");
	        		return;
	        	}
	        	vm.getDeptSickUpdate();
	            layer.open({
	                type: 1,
	                offset: '50px',
	                skin: 'layui-layer-molv',
	                title: "选择病区",
	                area: ['300px', '450px'],
	                shade: 0,
	                shadeClose: false,
	                content: jQuery("#deptLayerSick"),
	                btn: ['确定', '取消'],
	                btn1: function (index) {
	                    var node = ztreeSick.getSelectedNodes();
	                    //选择上级组织
	                    if(node.length>0){
	                    	  vm.schedule.sickAreaNO = node[0].deptId;
	                          vm.schedule.sickAreaName = node[0].name;
	                          vm.queryBedNOList();
	                          vm.schedule.bedNO=-1;
	                    }
	                      
	                    layer.close(index);
	                }
	            });
	        }
	}
});